package com.hzy.oms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.hzy.oms.domain.bo.OrderDeliveryHistoryBo;
import com.hzy.oms.domain.vo.OrderDeliveryHistoryVo;
import com.hzy.oms.domain.OrderDeliveryHistory;
import com.hzy.oms.mapper.OrderDeliveryHistoryMapper;
import com.hzy.oms.service.IOrderDeliveryHistoryService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 订单发货记录Service业务层处理
 *
 * @author zengxin
 * @date 2023-10-18
 */
@RequiredArgsConstructor
@Service
public class OrderDeliveryHistoryServiceImpl implements IOrderDeliveryHistoryService {

    private final OrderDeliveryHistoryMapper baseMapper;

    /**
     * 查询订单发货记录
     */
    @Override
    public OrderDeliveryHistoryVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询订单发货记录列表
     */
    @Override
    public TableDataInfo<OrderDeliveryHistoryVo> queryPageList(OrderDeliveryHistoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OrderDeliveryHistory> lqw = buildQueryWrapper(bo);
        Page<OrderDeliveryHistoryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询订单发货记录列表
     */
    @Override
    public List<OrderDeliveryHistoryVo> queryList(OrderDeliveryHistoryBo bo) {
        LambdaQueryWrapper<OrderDeliveryHistory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OrderDeliveryHistory> buildQueryWrapper(OrderDeliveryHistoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OrderDeliveryHistory> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getOrderId() != null, OrderDeliveryHistory::getOrderId, bo.getOrderId());
        lqw.eq(StringUtils.isNotBlank(bo.getDeliveryCompany()), OrderDeliveryHistory::getDeliveryCompany, bo.getDeliveryCompany());
        lqw.eq(StringUtils.isNotBlank(bo.getDeliverySn()), OrderDeliveryHistory::getDeliverySn, bo.getDeliverySn());
        return lqw;
    }

    /**
     * 新增订单发货记录
     */
    @Override
    public Boolean insertByBo(OrderDeliveryHistoryBo bo) {
        OrderDeliveryHistory add = BeanUtil.toBean(bo, OrderDeliveryHistory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改订单发货记录
     */
    @Override
    public Boolean updateByBo(OrderDeliveryHistoryBo bo) {
        OrderDeliveryHistory update = BeanUtil.toBean(bo, OrderDeliveryHistory.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(OrderDeliveryHistory entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除订单发货记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
